#include <iostream>
#include <thread>
using namespace std;

void func()
{
    for (int i = 0; i < 10; ++i)
    {

        this_thread::sleep_for(chrono::milliseconds(1));
        /*
        constexpr chrono::duration<long double>
        operator""s(long double __secs)
        { return chrono::duration<long double>{__secs}; }
    */
        this_thread::sleep_for(1us); // h/min/s/ms/us/ns

        // 获取当前系统时间点
        auto now = chrono::system_clock::now();
        time_t tm = chrono::system_clock::to_time_t(now);
        cout << "now: " << ctime(&tm);
        // 时间间隔为2s
        // 当前时间点之后休眠两秒
        this_thread::sleep_until(now + 1ms);
        now = chrono::system_clock::now();
        tm = chrono::system_clock::to_time_t(now);
        cout << "now: " << ctime(&tm);

        cout << "子线程: " << this_thread::get_id() << ", i = " << i << endl;
    }
}

void func2()
{
    for (int i = 0; i < 1010000000; ++i)
    {
        cout << "子线程: " << this_thread::get_id() << ", i = " << i << endl;
        this_thread::yield();
    }
}


int main()
{
    cout << "主线程: " << this_thread::get_id() << endl;
    thread t(func);
    t.join();

    thread t1(func2);
    thread t2(func2);
    t1.join();
    t2.join();
}


/*作者: 苏丙榅
链接: https://subingwen.cn/cpp/this_thread/
来源: 爱编程的大丙
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。*/